/*
 * @Author: your name
 * @Date: 2021-01-29 15:08:34
 * @LastEditTime: 2021-02-01 15:58:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wechat-node\utils\utils.js
 */
function checkSession(ctx) {
    if (!!ctx.session.sessionData) {
        return true;
    } else {
        return false;
    }
} 
function setSession(ctx, params) {
    if (!params) {
        ctx.session.sessionData = null;
    } else {
        const currentSession = ctx.session.sessionData || {};
        ctx.session.sessionData = {
            ...currentSession,
            ...params,
        };
    }
}

// http参数解析
function getRequestParams(ctx){
    var headerParams = ctx.request.query;
    var bodyParams = ctx.request.body;
    var params = ctx.params || {};
    var files = ctx.request.files;
    if((ctx.headers['content-type'] || '').indexOf('application/json') >= 0 && typeof(bodyParams) === 'string') bodyParams = JSON.parse(bodyParams);

    return Object.assign({}, headerParams, bodyParams, params, {files: files});
}

module.exports = {
    checkSession,
    setSession,
    getRequestParams,
}